package com.yanfan.ba;

import com.alibaba.fastjson2.JSONObject;
import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.RemoteDevice;
import com.serotonin.bacnet4j.exception.BACnetException;
import com.serotonin.bacnet4j.exception.PropertyValueException;
import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
import com.serotonin.bacnet4j.transport.DefaultTransport;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
import com.serotonin.bacnet4j.util.PropertyReferences;
import com.serotonin.bacnet4j.util.PropertyValues;
import com.serotonin.bacnet4j.util.ReadListener;
import com.serotonin.bacnet4j.util.RequestUtils;
import com.yanfan.ba.domain.NjBaDevice;
import com.yanfan.ba.domain.NjBaDeviceData;
import com.yanfan.ba.service.INjBaDeviceDataService;
import com.yanfan.ba.service.INjBaDeviceService;
import com.yanfan.ba.utils.StringExtractor;
import com.yanfan.ba.utils.StringUtil;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.stream.Collectors;

@Component("baTask")
public class BacnetDataRetriever {

    @Resource
    private INjBaDeviceDataService baDeviceDataService;
    @Resource
    private INjBaDeviceService baDeviceService;

    @Test
    public void getBaDeviceData() throws Exception {
        LocalDevice localDevice = null;
        try {
            // 创建网络对象
            IpNetwork ipNetwork = new IpNetworkBuilder()
                    .withLocalBindAddress("192.168.254.7")  // 本地IP地址
                    .withSubnet("255.255.255.0", 24)        // 本地子网掩码
                    .withPort(47808)                        // 本地端口
                    .withReuseAddress(true)
                    .build();
            // 创建虚拟的本地设备并初始化
            localDevice = new LocalDevice(223, new DefaultTransport(ipNetwork));
            localDevice.initialize();
            // 在初始化LocalDevice后，再注册为外部设备
            ipNetwork.registerAsForeignDevice(new InetSocketAddress("192.168.23.100", 47808), 10000000);
            ipNetwork.enableBBMD(); // 启用BBMD功能

            localDevice.startRemoteDeviceDiscovery();

            // 获取远程设备
            Integer remoteDeviceId = 2605;
            RemoteDevice remoteDevice = localDevice.getRemoteDeviceBlocking(remoteDeviceId);

            // 获取远程设备的标识符对象
            List<ObjectIdentifier> objectList = RequestUtils.getObjectList(localDevice, remoteDevice).getValues();

            List<ObjectIdentifier> aiList = new ArrayList<>();
            List<ObjectIdentifier> avList = new ArrayList<>();
            List<ObjectIdentifier> bvList = new ArrayList<>();


            // 过滤对象标识符列表
            for (ObjectIdentifier oi : objectList) {
                //System.out.println(oi);
                if (oi.getObjectType().equals(ObjectType.analogValue)) {
                    aiList.add(new ObjectIdentifier(ObjectType.analogValue, oi.getInstanceNumber()));
                }
                if (oi.getObjectType().equals(ObjectType.binaryOutput)) {
                    avList.add(new ObjectIdentifier(ObjectType.binaryOutput, oi.getInstanceNumber()));
                }
                if (oi.getObjectType().equals(ObjectType.binaryValue)) {
                    bvList.add(new ObjectIdentifier(ObjectType.binaryValue, oi.getInstanceNumber()));
                }
            }

            // 读取属性值
            PropertyValues pvAiObjectName = readValueByPropertyIdentifier(localDevice, remoteDevice, aiList, null, PropertyIdentifier.objectName);
            PropertyValues pvAiPresentValue = readValueByPropertyIdentifier(localDevice, remoteDevice, aiList, null, PropertyIdentifier.presentValue);
            PropertyValues pvAiDescription = readValueByPropertyIdentifier(localDevice, remoteDevice, aiList, null, PropertyIdentifier.description);

            PropertyValues pvAvObjectName = readValueByPropertyIdentifier(localDevice, remoteDevice, avList, null, PropertyIdentifier.objectName);
            PropertyValues pvAvPresentValue = readValueByPropertyIdentifier(localDevice, remoteDevice, avList, null, PropertyIdentifier.presentValue);
            PropertyValues pvAvDescription = readValueByPropertyIdentifier(localDevice, remoteDevice, avList, null, PropertyIdentifier.description);

            PropertyValues bvObjectName = readValueByPropertyIdentifier(localDevice, remoteDevice, bvList, null, PropertyIdentifier.objectName);
            PropertyValues bvPresentValue = readValueByPropertyIdentifier(localDevice, remoteDevice, bvList, null, PropertyIdentifier.presentValue);
            PropertyValues bvDescription = readValueByPropertyIdentifier(localDevice, remoteDevice, bvList, null, PropertyIdentifier.description);

            List<NjBaDeviceData> avDataList = new ArrayList<>();
            Set<NjBaDevice> deviceNameList = new HashSet<>();
            HashMap<String, NjBaDeviceData> map = new HashMap<>();
            HashMap<String, NjBaDeviceData> jskMap = new HashMap<>();
            HashMap<String, NjBaDevice> deviceMap = new HashMap<>();
            //获取所有的---集水坑
            for (ObjectIdentifier oi : aiList) {
                String description = pvAiDescription.get(oi, PropertyIdentifier.description).toString();
                String lastDelimiter = StringUtil.getLastDelimiter(description);
                //截取前半部分
                String dot = StringUtil.
                        getSubstringBeforeLastDelimiter(description, lastDelimiter);
                //截取后半部分
                String dot2 = StringUtil.
                        getSubstringAfterLastDelimiter(description, lastDelimiter);
                String dot1 = dot.replaceAll("[-_]", "_");
                String s = StringUtil.classifyAndStore(dot1);
                NjBaDeviceData data1 = jskMap.get(s);
                NjBaDeviceData jsk = new NjBaDeviceData();
                if (s != null) { //说明是17#集水坑类似的
                    if (data1 == null) {
                        jsk.setDeviceName(s);
                        jsk.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                        jsk.setObjectType(oi.getObjectType().toString());
                        switch (dot2) {
                            case "高液位":
                                jsk.setJsGValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                jsk.setJsDValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "设定CO浓度":
                                break;
                            default:
                                // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                break;
                        }
                        jskMap.put(s, jsk);
                    } else {
                        switch (dot2) {
                            case "高液位":
                                data1.setJsGValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                data1.setJsDValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "设定CO浓度":
                                break;
                            default:
                                // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                break;
                        }
                    }
                }
            }
            for (ObjectIdentifier oi : aiList) {
            /*NjBaDeviceData deviceData = new NjBaDeviceData();
            deviceData.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
            deviceData.setObjectType(oi.getObjectType().toString());
            deviceData.setInstanceNumber(oi.getInstanceNumber());
            deviceData.setTagNumber(pvAiObjectName.get(oi, PropertyIdentifier.objectName).toString());
            deviceData.setDescription(pvAiDescription.get(oi, PropertyIdentifier.description).toString());*/
                String description = pvAiDescription.get(oi, PropertyIdentifier.description).toString();
                String lastDelimiter = StringUtil.getLastDelimiter(description);
                //截取前半部分
                String dot = StringUtil.
                        getSubstringBeforeLastDelimiter(description, lastDelimiter);
                //截取后半部分
                String dot2 = StringUtil.
                        getSubstringAfterLastDelimiter(description, lastDelimiter);
                String dot1 = dot.replaceAll("[-_]", "_");
                NjBaDeviceData data = map.get(dot1);
                NjBaDevice njBaDevice = deviceMap.get(dot1);
                NjBaDevice baDevice = new NjBaDevice();
                NjBaDeviceData deviceData = new NjBaDeviceData();
                String s = StringUtil.classifyAndStore(dot1);
                NjBaDeviceData data1 = jskMap.get(s);
                NjBaDeviceData jsk = new NjBaDeviceData();
                //设备
                if (StringUtils.isBlank(s)) {
                    if (!dot.equals("设定CO浓度")) {
                        if (njBaDevice == null) {
                            baDevice.setDeviceName(dot1);
                            baDevice.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            switch (dot2) {
                                case "运行状态":
                                    baDevice.setRunStatus(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                            String type = StringUtil.getFirstPart(baDevice.getDeviceName());
                            String deviceType = null;
                            if (type.matches(".*ZS$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "普通风机";
                            } else if (type.matches(".*生活水箱$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "生活水箱";
                            } else {
                                switch (type) {
                                    case "PYF":
                                        deviceType = "排烟风机";
                                        break;
                                    case "SYF":
                                        deviceType = "送烟风机";
                                        break;
                                    case "PF":
                                        deviceType = "排风机";
                                        break;
                                    case "ZS":
                                        deviceType = "普通风机";
                                        break;
                                    default:
                                        deviceType = "集水坑";
                                        break;
                                }
                            }
                            baDevice.setDeviceType(deviceType);
                            deviceMap.put(dot1, baDevice);
                        } else {
                            switch (dot2) {
                                case "运行状态":
                                    njBaDevice.setRunStatus(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                        }
                    }
                }
                //设备数据
                if (StringUtils.isBlank(s)) {
                    if (!dot.equals("设定CO浓度")) {
                        String s1 = dot1.split("_")[0];
                        NjBaDeviceData data2 = jskMap.get(s1);
                        if (data == null) {
                            deviceData.setDeviceName(dot1);
                            deviceData.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            deviceData.setObjectType(oi.getObjectType().toString());
                            deviceData.setUpdateTime(DateUtils.getTime());
                            baDevice.setDeviceName(dot1);
                            baDevice.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            if (data2 != null) {
                                deviceData.setJsGValue(data2.getJsGValue());
                                deviceData.setJsDValue(data2.getJsDValue());
                            }
                            switch (dot2) {
                                case "CO":
                                    deviceData.setPresentValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "COa":
                                    deviceData.setPresentValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "启停控制":
                                    deviceData.setControlValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "运行状态":
                                    deviceData.setRunValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    baDevice.setRunStatus(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "故障报警":
                                    deviceData.setFaultValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "手自动反馈":
                                    deviceData.setFeedbackValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                            /*case "高液位":
                                deviceData.setJsGValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                deviceData.setJsDValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;*/
                                case "设定CO浓度":
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                            String type = StringUtil.getFirstPart(deviceData.getDeviceName());
                            String deviceType = null;
                            if (type.matches(".*ZS$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "普通风机";
                            } else if (type.matches(".*生活水箱$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "生活水箱";
                            } else {
                                switch (type) {
                                    case "PYF":
                                        deviceType = "排烟风机";
                                        break;
                                    case "SYF":
                                        deviceType = "送烟风机";
                                        break;
                                    case "PF":
                                        deviceType = "排风机";
                                        break;
                                    case "ZS":
                                        deviceType = "普通风机";
                                        break;
                                    default:
                                        deviceType = "集水坑";
                                        break;
                                }
                            }
                            deviceData.setDeviceType(deviceType);
                            baDevice.setDeviceType(deviceType);
                            baDevice.setRunStatus(deviceData.getRunValue());
                            map.put(dot1, deviceData);
                            deviceMap.put(dot1, baDevice);
                        } else {
                            if (data2 != null) {
                                data.setJsGValue(data2.getJsGValue());
                                data.setJsDValue(data2.getJsDValue());
                            }
                   /* data.setDeviceName(dot1);
                    data.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                    data.setObjectType(oi.getObjectType().toString());
                    data.setUpdateTime(DateUtils.getTime());*/
                            switch (dot2) {
                                case "CO":
                                    data.setPresentValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "COa":
                                    deviceData.setPresentValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "启停控制":
                                    data.setControlValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "运行状态":
                                    data.setRunValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    //baDevice.setRunStatus(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "故障报警":
                                    data.setFaultValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "手自动反馈":
                                    data.setFeedbackValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                            /*case "高液位":
                                data.setJsGValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                data.setJsDValue(pvAiPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;*/
                                case "设定CO浓度":
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                        }
                    }
                }
            }

            for (ObjectIdentifier oi : avList) {
                NjBaDeviceData deviceData = new NjBaDeviceData();
                deviceData.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                deviceData.setObjectType(oi.getObjectType().toString());
                deviceData.setInstanceNumber(oi.getInstanceNumber());
                deviceData.setTagNumber(pvAvObjectName.get(oi, PropertyIdentifier.objectName).toString());
                deviceData.setPresentValue(pvAvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                deviceData.setDescription(pvAvDescription.get(oi, PropertyIdentifier.description).toString());
                deviceData.setUpdateTime(DateUtils.getTime());
                avDataList.add(deviceData);
            }
            //获取所有的---集水坑
            for (ObjectIdentifier oi : bvList) {
                String description = bvDescription.get(oi, PropertyIdentifier.description).toString();
                String lastDelimiter = StringUtil.getLastDelimiter(description);
                //截取前半部分
                String dot = StringUtil.
                        getSubstringBeforeLastDelimiter(description, lastDelimiter);
                //截取后半部分
                String dot2 = StringUtil.
                        getSubstringAfterLastDelimiter(description, lastDelimiter);

                String dot1 = dot.replaceAll("[-_]", "_");
                NjBaDeviceData data = map.get(dot1);
                NjBaDevice njBaDevice = deviceMap.get(dot1);
                NjBaDevice baDevice = new NjBaDevice();
                NjBaDeviceData deviceData = new NjBaDeviceData();
                String s = StringUtil.classifyAndStore(dot1);
                NjBaDeviceData data1 = jskMap.get(s);
                NjBaDeviceData jsk = new NjBaDeviceData();
                if (s != null) { //说明是17#集水坑类似的
                    if (data1 == null) {
                        jsk.setDeviceName(s);
                        jsk.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                        jsk.setObjectType(oi.getObjectType().toString());
                        switch (dot2) {
                            case "高液位":
                                jsk.setJsGValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                jsk.setJsDValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "设定CO浓度":
                                break;
                            default:
                                // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                break;
                        }
                        jskMap.put(s, jsk);
                    } else {
                        switch (dot2) {
                            case "高液位":
                                data1.setJsGValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                data1.setJsDValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "设定CO浓度":
                                break;
                            default:
                                // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                break;
                        }
                    }
                }
            }
            for (ObjectIdentifier oi : bvList) {
            /*NjBaDeviceData deviceData = new NjBaDeviceData();
            deviceData.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
            deviceData.setObjectType(oi.getObjectType().toString());
            deviceData.setInstanceNumber(oi.getInstanceNumber());
            deviceData.setTagNumber(bvObjectName.get(oi, PropertyIdentifier.objectName).toString());
            deviceData.setPresentValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
            deviceData.setDescription(bvDescription.get(oi, PropertyIdentifier.description).toString());
            deviceData.setUpdateTime(DateUtils.getTime());
            String lastDelimiter = StringUtil.getLastDelimiter(deviceData.getDescription());
            String dot1 = StringUtil.getSubstringBeforeLastDelimiter(deviceData.getDescription(), lastDelimiter);
            deviceData.setDeviceName(dot1);*/
                String description = bvDescription.get(oi, PropertyIdentifier.description).toString();
                String lastDelimiter = StringUtil.getLastDelimiter(description);
                //截取前半部分
                String dot = StringUtil.
                        getSubstringBeforeLastDelimiter(description, lastDelimiter);
                //截取后半部分
                String dot2 = StringUtil.
                        getSubstringAfterLastDelimiter(description, lastDelimiter);

                String dot1 = dot.replaceAll("[-_]", "_");
                NjBaDeviceData data = map.get(dot1);
                NjBaDevice njBaDevice = deviceMap.get(dot1);
                NjBaDevice baDevice = new NjBaDevice();
                NjBaDeviceData deviceData = new NjBaDeviceData();
                String s = StringUtil.classifyAndStore(dot1);
                NjBaDeviceData data1 = jskMap.get(s);
                NjBaDeviceData jsk = new NjBaDeviceData();
                //设备
                if (StringUtils.isBlank(s)) {
                    if (!dot.equals("设定CO浓度")) {
                        if (njBaDevice == null) {
                            baDevice.setDeviceName(dot1);
                            baDevice.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            switch (dot2) {
                                case "运行状态":
                                    baDevice.setRunStatus(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                            String type = StringUtil.getFirstPart(baDevice.getDeviceName());
                            String deviceType = null;
                            if (type.matches(".*ZS$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "普通风机";
                            } else if (type.matches(".*生活水箱$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "生活水箱";
                            } else {
                                switch (type) {
                                    case "PYF":
                                        deviceType = "排烟风机";
                                        break;
                                    case "SYF":
                                        deviceType = "送烟风机";
                                        break;
                                    case "PF":
                                        deviceType = "排风机";
                                        break;
                                    case "ZS":
                                        deviceType = "普通风机";
                                        break;
                                    default:
                                        deviceType = "集水坑";
                                        break;
                                }
                            }
                            baDevice.setDeviceType(deviceType);
                            deviceMap.put(dot1, baDevice);
                        } else {
                            switch (dot2) {
                                case "运行状态":
                                    njBaDevice.setRunStatus(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                default:
                                    // 如果没有匹配的情况，可以选择添加一个默认处理逻辑
                                    break;
                            }
                        }
                        //baDevice.setRunStatus(deviceData.getRunValue());
                    }
                }
                //设备数据
                if (StringUtils.isBlank(s)) {
                    if (!dot.equals("设定CO浓度")) {
                        String s1 = dot1.split("_")[0];
                        NjBaDeviceData data2 = jskMap.get(s1);
                        if (data == null) {
                            deviceData.setDeviceName(dot1);
                            deviceData.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            deviceData.setObjectType(oi.getObjectType().toString());
                            deviceData.setUpdateTime(DateUtils.getTime());
                            baDevice.setDeviceName(dot1);
                            baDevice.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                            if (data2 != null) {
                                deviceData.setJsGValue(data2.getJsGValue());
                                deviceData.setJsDValue(data2.getJsDValue());
                            }
                            switch (dot2) {
                                case "CO":
                                    deviceData.setPresentValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "COa":
                                    deviceData.setPresentValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "启停控制":
                                    deviceData.setControlValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "运行状态":
                                    deviceData.setRunValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    baDevice.setRunStatus(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "故障报警":
                                    deviceData.setFaultValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "手自动反馈":
                                    deviceData.setFeedbackValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                           /* case "高液位":
                                deviceData.setJsGValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                deviceData.setJsDValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;*/
                                case "设定CO浓度":
                                    break;
                                default:
                                    // 可在此处添加默认处理逻辑
                                    break;
                            }
                            String type = StringUtil.getFirstPart(deviceData.getDeviceName());
                            String deviceType = null;
                            if (type.matches(".*ZS$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "普通风机";
                            } else if (type.matches(".*生活水箱$")) {  // 检查字符串是否以 "ZS" 结尾
                                deviceType = "生活水箱";
                            } else {
                                switch (type) {
                                    case "PYF":
                                        deviceType = "排烟风机";
                                        break;
                                    case "SYF":
                                        deviceType = "送烟风机";
                                        break;
                                    case "PF":
                                        deviceType = "排风机";
                                        break;
                                    case "ZS":
                                        deviceType = "普通风机";
                                        break;
                                    default:
                                        deviceType = "集水坑";
                                        break;
                                }
                            }
                            deviceData.setDeviceType(deviceType);
                            baDevice.setDeviceType(deviceType);
                            baDevice.setRunStatus(deviceData.getRunValue());
                            map.put(dot1, deviceData);
                            deviceMap.put(dot1, baDevice);
                        } else {
                            if (data2 != null) {
                                data.setJsGValue(data2.getJsGValue());
                                data.setJsDValue(data2.getJsDValue());
                            }
                    /*data.setDeviceName(dot1);
                    data.setId(oi.getObjectType().toString() + oi.getInstanceNumber());
                    data.setObjectType(oi.getObjectType().toString());
                    data.setUpdateTime(DateUtils.getTime());*/
                            switch (dot2) {
                                case "CO":
                                    data.setPresentValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "COa":
                                    deviceData.setPresentValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "启停控制":
                                    data.setControlValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "运行状态":
                                    data.setRunValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    //baDevice.setRunStatus(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "故障报警":
                                    data.setFaultValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                                case "手自动反馈":
                                    data.setFeedbackValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                    break;
                           /* case "高液位":
                                data.setJsGValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;
                            case "低液位":
                                data.setJsDValue(bvPresentValue.get(oi, PropertyIdentifier.presentValue).toString());
                                break;*/
                                case "设定CO浓度":
                                    break;
                                default:
                                    // 可在此处添加默认处理逻辑
                                    break;
                            }
                        }
                        //baDevice.setRunStatus(deviceData.getRunValue());
                    }
                }

                //avDataList.add(deviceData);
            /* NjBaDevice njBaDevice = new NjBaDevice();
            njBaDevice.setDeviceName(dot1);
            deviceNameList.add(njBaDevice);*/
            }
            List<NjBaDeviceData> valueList = map.values().stream().collect(Collectors.toList());
            List<NjBaDevice> deviceList = deviceMap.values().stream().collect(Collectors.toList());
            baDeviceDataService.saveOrUpdateBatch(valueList);
            baDeviceService.saveOrUpdateBatch(deviceList);
        } catch (BACnetException e) {
            System.err.println("BACnet异常: " + e.getMessage());
            e.printStackTrace();
        } finally {
            if (localDevice != null) {
                try {
                    localDevice.terminate();
                } catch (Exception ex) {
                    System.err.println("关闭本地设备时出错: " + ex.getMessage());
                }
            }
        }
        // 关闭本地设备
        //localDevice.terminate();

    }

    public static PropertyValues readValueByPropertyIdentifier(final LocalDevice localDevice, final RemoteDevice d,
                                                               final List<ObjectIdentifier> ois, final ReadListener callback, PropertyIdentifier propertyIdentifier) throws BACnetException {
        if (ois.size() == 0) {
            return new PropertyValues();
        }

        final PropertyReferences refs = new PropertyReferences();
        for (final ObjectIdentifier oid : ois) {
            refs.add(oid, propertyIdentifier);
        }

        return RequestUtils.readProperties(localDevice, d, refs, false, callback);
    }
}


